
 var apiHost = wx.getStorageSync("apiHost");    //请求地址
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showProperName : '',                    //规格名称
    nameList: [],                           //类型名称
    categoryList:[],                        //分类集合
    categoryNumList: [],                    //类别判断        
    addCategoryBtnNum : 0,                  //添加分类按钮标识
    pickerid :'',
    spuCategoryId : '',                     //类别id
    categoryFirst : '',                     //类别第一输入框
    categoryInput0: '',                     //类别第二输入框
    categoryInput1: '',                     //类别第三输入框
    categoryInput2: '',                     //类别第四输入框
    collageCategoryId : '',                 //规格id(用作判断是添加还是编辑)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var collageId = wx.getStorageSync('collageId');      //拼团id
    var userId = wx.getStorageSync('userId');         //用户id
    // var userId = '41896a7744944b6bba6da2a0e0671c3f'      //用户id


    var collageCategoryId = options.collageCategoryId;
    //判断是添加还是编辑
    if (collageCategoryId !='undefined'){
      that.skuDetailPage(collageCategoryId);  //规格详情页
      that.setData({
        collageCategoryId: collageCategoryId
      })
    }
    that.setData({
      collageId: collageId,
      userId: userId
    })
    
    that.getCategoryList();
  },

  /**
   * 获取分类信息
   */
  getCategoryList : function(){
    var that = this ; 
    wx.showLoading({
      title: '玩命加载中',
      icon: 'loading',
      mask: true
    });

    wx.request({
      url: apiHost + 'hpp/spu/category/list',
      method:'post',
      header:{
        "content-type" : "application/json"
      },
      success : function(res){

        var code = res.data.code;
        var msg = res.data.msg;

        if ("10000" == code){
          var categoryList = res.data.data;

          for (var i = 0; i < categoryList.length;i++){
            that.data.nameList.push(categoryList[i].name)
          }

          that.setData({
            nameList: that.data.nameList,
            categoryList: categoryList
          });
          wx.hideLoading();
        }else{
          wx.hideLoading();
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
      },
      fail : function(){
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask: true
        });
      }
    })
  },

  /**
   * 点击选择名称
   */
  bindPickerChange: function (e) {
    var that = this;
    var pickerNum = e.detail.value;
    var categoryList = that.data.categoryList;
    
    // 选择名称 和 id
    var pickerName = that.data.nameList[pickerNum];
    var pickerid = that.data.categoryList[pickerNum].id;
    
    that.setData({
      showProperName: pickerName,
      pickerid: pickerid
    })
  },

  /**
   * 首个分类 值
   */
  categoryFirst : function(e){
    var categoryFirst = e.detail.value;

    this.setData({
      categoryFirst: categoryFirst
    })
  },
  /**
   * 分类1
   */
  categoryInput0: function (e) {
    var categoryInput0 = e.detail.value;
    this.setData({
      'categoryNumList[0].value': categoryInput0
    })
  },
  /**
   * 分类2
   */
  categoryInput1: function (e) {
    var categoryInput1 = e.detail.value;
    this.setData({
      'categoryNumList[1].value': categoryInput1
    })
  },
  /**
   * 分类3
   */
  categoryInput2: function (e) {
    var categoryInput2 = e.detail.value;
    this.setData({
      'categoryNumList[2].value': categoryInput2
    })
  },

  
  /**
   * 添加类别
   */
  addCategory:function(e){
    var that = this;
    var addCategoryBtnNum = e.currentTarget.dataset.toggle;   //添加类别按钮个数
    var categoryNumFlag = addCategoryBtnNum + 1 ;             //计算数组的值

    var obj = {};
    obj.title = addCategoryBtnNum;
    obj.value = '';

    that.data.categoryNumList.push(obj);  //向数组里面添加

    // that.data.categoryNumList.push("" + categoryNumFlag+"");  //向数组里面添加


    var countCategory = that.data.categoryNumList.length;     //计算数组长度，用于类别按钮显示隐藏

    that.setData({
      addCategoryBtnNum : categoryNumFlag,
      categoryNumList: that.data.categoryNumList,
      countCategory: countCategory
    });
  },
  /**
 * 添加类别  默认添加
 */
  addCategoryFlag: function (btnNum) {
    var that = this;
    var addCategoryBtnNum = btnNum;   //添加类别按钮个数
    var categoryNumFlag = addCategoryBtnNum + 1;             //计算数组的值
    that.data.categoryNumList.push("" + categoryNumFlag + "");  //向数组里面添加
    var countCategory = that.data.categoryNumList.length;     //计算数组长度，用于类别按钮显示隐藏

    that.setData({
      addCategoryBtnNum: categoryNumFlag,
      categoryNumList: that.data.categoryNumList,
      countCategory: countCategory
    });
  },
  /**
   * 删除类别
   */
  delCategory:function(e){
    var that = this;
    var delCategoryNum = e.currentTarget.dataset.toggle;   //添加类别按钮个数

    var indexAddBtn = delCategoryNum.split(",");
    var index = indexAddBtn[0];
    var nowBtn = indexAddBtn[1];

    that.delInputValue(index);      //清空输入框

    nowBtn = nowBtn - 1
    that.data.categoryNumList.splice(index, 1);
    var countCategory = that.data.categoryNumList.length;
    console.log(that.data.categoryNumList)

    that.setData({
      addCategoryBtnNum: nowBtn,
      categoryNumList: that.data.categoryNumList,
      countCategory: countCategory
    });
  },

  /**
   * 清空输入框
   */
  delInputValue : function(num){
    console.log(num)
    var that = this;
    if (num==0){
      that.setData({
        'categoryNumList[0].value' : ''
      });
    }
    if (num == 1) {
      that.setData({
        'categoryNumList[1].value': ''
      });
    }
    if (num == 2) {
      that.setData({
        'categoryNumList[2].value': ''
      });
    }
  },

  /**
   * 保存规格
   */
  checkProperty : function(){
    var that = this;
    
    var userId = that.data.userId;                //用户Id
    var collageId = that.data.collageId;          //拼团Id

    var collageCategoryId = that.data.collageCategoryId; //规格id （用作判断添加还是编辑）

    var spuCategoryId = that.data.pickerid;       //商品类型Id
    var categoryFirst = that.data.categoryFirst;  //第一个类别

    var categoryNumList = that.data.categoryNumList;

    var skuNames = '';

    if (spuCategoryId == '') {
      that.promptMsg("请选择名称");
      return;
    } else if(categoryFirst == '') {
      that.promptMsg("请输入分类");
      return;
    } 
     if (categoryFirst != '') {
      skuNames = categoryFirst;
    }  
     if (categoryNumList.length == 1) {
       var categoryInput0 = that.data.categoryNumList[0].value;//类别1
        skuNames = skuNames + ',' + categoryInput0 
    }  
     if (categoryNumList.length == 2) {
       var categoryInput0 = that.data.categoryNumList[0].value;//类别1
       var categoryInput1 = that.data.categoryNumList[1].value;//类别2
       skuNames = skuNames + ',' + categoryInput0 + ","+categoryInput1
    }  
     if (categoryNumList.length == 3) {
       var categoryInput0 = that.data.categoryNumList[0].value;//类别1
       var categoryInput1 = that.data.categoryNumList[1].value;//类别2
       var categoryInput2 = that.data.categoryNumList[2].value;//类别3
       skuNames = skuNames + ',' + categoryInput0 + ',' + categoryInput1 + "," + categoryInput2
    }

    //保存规格
    that.addProperty(userId, spuCategoryId, skuNames, collageId, collageCategoryId)
  },

  /**
   * 保存规格
   */
  addProperty: function (userId, spuCategoryId, skuNames, collageId, collageCategoryId){
    var that = this;

    wx.showLoading({
      title: '玩命加载中',
      icon: 'loading',
      mask: true
    });

    wx.request({
      url: apiHost + 'hpp/collage/addOrEditSku',
      method: 'get',
      header: {
        "content-type": "application/json"
      },
      data:{
        userId: userId,
        spuCategoryId: spuCategoryId,
        skuNames: skuNames,
        collageId: collageId,
        collageCategoryId: collageCategoryId
      },

      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        wx.showToast({
          title: msg,
          icon: 'none',
          duration: 2000,
          mask: true
        });

        //成功后跳转高级设置
        if ("10000" == code) {
          wx.reLaunch({
            url: '../seniorSetUp/seniorSetUp',
            success: function () {
              wx.hideLoading();   //隐藏加载loading
            },
          });
        }

      },
      fail: function () {
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask: true
        });
      }
    })
  },

  /**
   * 规格详情
   */
  skuDetailPage: function (collageCategoryId){
    var that = this;

    wx.showLoading({
      title: '玩命加载中',
      icon : 'loading',
      mask: true
    })

    wx.request({
      url: apiHost +'hpp/collage/skuDetailPage',
      method : 'get',
      header: {
        "content-type" : "application/json"
      },
      data :{
        collageCategoryId: collageCategoryId
      },
      success : function(res){
        var code = res.data.code;
        var msg = res.data.msg;

        if ("10000" == code){
          var spuCategoryName = res.data.data.spuCategoryName;    //规格名称
          var spuCategoryId = res.data.data.spuCategoryId;        //规格id

          var skuNames = res.data.data.skuNames; 

          var categoryFirst = skuNames[0].name;

          for (var i = 1; i < skuNames.length ; i++){
            that.addCategoryFlag(i);
            if (i==1){
              that.setData({
                'categoryNumList[0].value': skuNames[i].name
              })
            }
            if (i == 2) {
              that.setData({
                'categoryNumList[1].value': skuNames[i].name
              })
            }
            if (i == 3) {
              that.setData({
                'categoryNumList[2].value': skuNames[i].name
              })
            }
          }

          that.setData({
            categoryFirst: categoryFirst,
            showProperName: spuCategoryName,
            pickerid: spuCategoryId
          })
        }
      },
      fail : function(){

      }
    })
  },

  /**
   * 删除规格
   */
  delProperty : function(){
    var that = this;

    var collageId = that.data.collageId;                  //拼团iD
    var collageCategoryId = that.data.collageCategoryId;  //拼团属性iD
    
    wx.showLoading({
      title: '玩命加载中',
      icon:"loading",
      mask: true
    })
    wx.request({
      url: apiHost + 'hpp/collage/delCollageSku',
      method : 'get',
      header : {
        "content-type": "application/json"
      },
      data:{
        collageId: collageId,
        collageCategoryId: collageCategoryId
      },
      success : function(res){
        var code = res.data.code;
        var msg = res.data.msg;
        
        wx.hideLoading();

        wx.showToast({
          title: msg,
          icon: 'none',
          duration: 2000,
          mask: true,
          success : function(){
            if ("10000" == code) {
              wx.reLaunch({
                url: '../seniorSetUp/seniorSetUp',
                success: function () {
                  wx.hideLoading();   //隐藏加载loading
                },
              });
            }
          }
        });



      },
      fail : function(){
        wx.hideLoading();
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask: true
        });
      }
    })
  },

  /**
 * 提示信息
 */
  promptMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})